package com.itheima.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWmUserClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.itheima.user.mapper.ApUserMapper;
import com.itheima.user.mapper.ApUserRealnameMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 大可
 * @date 2022/7/29 20:18
 */
@Service
@Slf4j
public class ApUserRealnameServiceimpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private IWmUserClient iWmUserClient;

    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {
        //检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页检查条件
        dto.checkParam();
        //分页根据状态精准查询
        Page<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dto.getStatus() != null, ApUserRealname::getStatus, dto.getStatus());

        Page<ApUserRealname> page1 = apUserRealnameMapper.selectPage(page, wrapper);
        //返回结果
        Long total = page.getTotal();
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), total.intValue());
        //将page1页面内容的list集合赋值给responseResult
        responseResult.setData(page1.getRecords());
        return responseResult;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ResponseResult auth(AuthDto dto, Integer type) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        Integer id = dto.getId();

        ApUserRealname apUserRealname = apUserRealnameMapper.selectById(id);
        if (apUserRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        // 更新一下审核状态
        apUserRealname.setStatus((short) (type == 1 ? 9 : 2));
        apUserRealname.setId(id);
        apUserRealname.setReason(dto.getMsg());
        apUserRealnameMapper.updateById(apUserRealname);

        // 通过
        if (type == 1) {
            String name = apUserRealname.getName();

            Map<String, String> map = new HashMap<>();
            map.put("name", name);
            ResponseResult responseResult = iWmUserClient.addUser(map);
            if (responseResult == null || responseResult.getCode() != 200) {
                throw new RuntimeException("自媒体用户创建失败");
            }

            ApUser apUser = new ApUser();
            apUser.setId(apUserRealname.getUserId());
            apUser.setFlag((short) 1);
            int updateResult = apUserMapper.updateById(apUser);
            if (updateResult < 1) {
                throw new RuntimeException("APP用户状态更新失败");
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
